package com.sinosoft.hydra.um.services.component.bacup;

import org.springframework.beans.factory.config.AbstractFactoryBean;

import java.util.List;

/**
 * Created by Administrator on 2016/6/20.
 */
public class BakupFactoryBean extends AbstractFactoryBean<BakupWorker> {

    private List<BakupWorker> backUpServices;
    protected String url;
    protected String username;
    protected String password;
    protected String driverClassName;


    public Class<?> getObjectType() {
        return BakupWorker.class;
    }

    public static class DbInfo{
        protected String url;
        protected String username;
        protected String password;

        public DbInfo(String url, String username, String password) {
            this.url = url;
            this.username = username;
            this.password = password;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }


    protected BakupWorker createInstance() throws Exception {
        for (BakupWorker bakupWorker : backUpServices) {
            bakupWorker.init(url, username, password);
            if (bakupWorker.valid(driverClassName)) {
                return bakupWorker;
            }
        }
        return null;
    }

    public static interface BakupListener {
        public void bakupSuccess();

        public void bacupFail(Exception ex);
    }

    public static interface RecoveryListener {
        public void recoverySuccess();

        public void recoveryFail(Exception ex);
    }

    public void setBackUpServices(List<BakupWorker> backUpServices) {
        this.backUpServices = backUpServices;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}
